//
//  DeviceBMSViewController.m
//  sentorise
//
//  Created by admin on 2025/8/28.
//

#import "DeviceBMSViewController.h"
#import "GlobalDefine.h"
#import "BTManager.h"
#import "DeviceBMSTableViewCell.h"

@interface DeviceBMSViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *hardwareBtn;
@property (weak, nonatomic) IBOutlet UIButton *batteryBtn;

@property (nonatomic,strong) NSMutableArray *warningArray;

@end

@implementation DeviceBMSViewController
{
    NSArray *redGreenAlert;
    NSArray *itemNameArray;
    int currentStatus;//0:硬件错误 1:电池保护
    bool isBriefMode;
    int tableClickCount;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.topSpace.constant = IphoneX ? 54 : 20;
    
    self.titleLabel.text = NSLocalizedString(@"Warning message", nil);
    [self.hardwareBtn setTitle:NSLocalizedString(@"Hardware Alarm", nil) forState:UIControlStateNormal];
    [self.batteryBtn setTitle:NSLocalizedString(@"Battery Alarm", nil) forState:UIControlStateNormal];

    isBriefMode = true;
    currentStatus = 0;
    redGreenAlert = [NSArray arrayWithObjects:[UIImage imageNamed:@"green"],[UIImage imageNamed:@"red"], nil];
    itemNameArray = [NSArray arrayWithObjects:
                     NSLocalizedString(@"ComError_AFE1", nil),
                     NSLocalizedString(@"ComError_AFE2", nil),
                     NSLocalizedString(@"ComError_Can", nil),
                     NSLocalizedString(@"ComError_E2P", nil),
                     NSLocalizedString(@"ComError_SPI", nil),
                     NSLocalizedString(@"ComError_Upper", nil),
                     NSLocalizedString(@"ComError_Client1", nil),
                     NSLocalizedString(@"ComError_Screen", nil),
                     NSLocalizedString(@"ComError_Wifi", nil),
                     NSLocalizedString(@"Bluetoothdevice", nil),
                     NSLocalizedString(@"ComError_App", nil),
                     NSLocalizedString(@"Error_CBC_CHG", nil),
                     NSLocalizedString(@"Error_StoreE2P", nil),
                     NSLocalizedString(@"Error_HSE", nil),
                     NSLocalizedString(@"Error_LSE", nil),
                     NSLocalizedString(@"Error_Flash", nil),
                     NSLocalizedString(@"Error_ADC", nil),
                     NSLocalizedString(@"Heating", nil),
                     NSLocalizedString(@"Condensation", nil),
                     NSLocalizedString(@"Res5", nil),
                     NSLocalizedString(@"Res6", nil),
                     NSLocalizedString(@"Error_CBC_DSG", nil), nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alarmDataArrive:) name:@"alarmdata" object:nil];

    [self.tableView registerNib:[UINib nibWithNibName:@"DeviceBMSTableViewCell" bundle:nil] forCellReuseIdentifier:@"DeviceBMSTableViewCell"];
}

- (void)alarmDataArrive:(NSNotification*)noti {
    _warningArray = noti.object;
    [self.tableView reloadData];
}

- (IBAction)actionBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)actionSeg:(UIButton *)sender {
    if ([sender isEqual:self.hardwareBtn]) {
        self.hardwareBtn.backgroundColor = RGB16(0x94C949);
        self.batteryBtn.backgroundColor = [UIColor clearColor];
        itemNameArray = [NSArray arrayWithObjects:
                         NSLocalizedString(@"ComError_AFE1", nil),
                         NSLocalizedString(@"ComError_AFE2", nil),
                         NSLocalizedString(@"ComError_Can", nil),
                         NSLocalizedString(@"ComError_E2P", nil),
                         NSLocalizedString(@"ComError_SPI", nil),
                         NSLocalizedString(@"ComError_Upper", nil),
                         NSLocalizedString(@"ComError_Client1", nil),
                         NSLocalizedString(@"ComError_Screen", nil),
                         NSLocalizedString(@"ComError_Wifi", nil),
                         NSLocalizedString(@"Bluetoothdevice", nil),
                         NSLocalizedString(@"ComError_App", nil),
                         NSLocalizedString(@"Error_CBC_CHG", nil),
                         NSLocalizedString(@"Error_StoreE2P", nil),
                         NSLocalizedString(@"Error_HSE", nil),
                         NSLocalizedString(@"Error_LSE", nil),
                         NSLocalizedString(@"Error_Flash", nil),
                         NSLocalizedString(@"Error_ADC", nil),
                         NSLocalizedString(@"Heating", nil),
                         NSLocalizedString(@"Condensation", nil),
                         NSLocalizedString(@"Res5", nil),
                         NSLocalizedString(@"Res6", nil),
                         NSLocalizedString(@"Error_CBC_DSG", nil), nil];
        currentStatus = 0;
    }else {
        self.hardwareBtn.backgroundColor = [UIColor clearColor];
        self.batteryBtn.backgroundColor = RGB16(0x94C949);
        itemNameArray = [NSArray arrayWithObjects:
                         NSLocalizedString(@"OV_Cell", nil) ,
                         NSLocalizedString(@"UV_Cell", nil),
                         NSLocalizedString(@"OV_Bat", nil),
                         NSLocalizedString(@"UV_Bat", nil),
                         NSLocalizedString(@"OC_Chg", nil),
                         NSLocalizedString(@"OC_Dsg", nil),
                         NSLocalizedString(@"OT_Chg", nil),
                         NSLocalizedString(@"UT_Chg", nil),
                         NSLocalizedString(@"OT_Dsg", nil),
                         NSLocalizedString(@"UT_Dsg", nil),
                         NSLocalizedString(@"Vdelta_0p", nil),
//                         NSLocalizedString(@"Res", nil),
//                         NSLocalizedString(@"OT_MOS", nil),
//                         NSLocalizedString(@"SOC_low_P", nil),
//                         NSLocalizedString(@"Balanced_Cnt", nil),
                         nil];
        currentStatus = 1;
    }
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return currentStatus==0 ? MIN(6, itemNameArray.count) : itemNameArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DeviceBMSTableViewCell *cell = (DeviceBMSTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"DeviceBMSTableViewCell"];
    NSString *valueString;
    NSInteger value;
    NSInteger realPosition = indexPath.row;
    if (currentStatus==0) {
        if (isBriefMode) {
            if (isBriefMode) {
                switch (indexPath.row) {
                    case 2:
                        realPosition = 3;
                        break;
                    case 3:
                        realPosition = 11;
                        break;
                    case 4:
                        realPosition = 12;
                        break;
                    case 5:
                        realPosition = 21;
                        break;
                }
            }
            valueString=[_warningArray objectAtIndex:realPosition];
        }else {
            valueString=[_warningArray objectAtIndex:indexPath.row];
        }
    }else {
        valueString=[_warningArray objectAtIndex:indexPath.row+22];
    }
    
    value = valueString.integerValue;
    [cell.iconIV setImage:redGreenAlert[value>0]];
    cell.numsLabel.text = valueString ?: @"0";
    cell.titleLabel.text = itemNameArray[realPosition];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 36;
}


@end
